package com.zj.blockchain.runner;

import com.zj.blockchain.util.BlockCache;
import com.zj.blockchain.websocket.P2pClient;
import com.zj.blockchain.websocket.P2pServer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

/**
 * @className: P2pRunner
 * @description: TODO 类描述
 * @author: allnas
 * @date: 2020/3/1
 **/
@Slf4j
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class P2pRunner implements ApplicationRunner {

    private final BlockCache blockCache;

    private final P2pServer p2pServer;

    private final P2pClient p2pClient;

    @Override
    public void run(ApplicationArguments args) {
        int p2pPort = blockCache.getP2pPort();
        String address = blockCache.getAddress();
        log.info("p2pPort:[{}],address:[{}]", p2pPort, address);
        p2pServer.initP2PServer(p2pPort);
        p2pClient.connectToPeer(address);
    }
}
